package com.ge.modules.api.secondController;

import com.ge.common.annotation.LoginTechnicianApi;
import com.ge.common.utils.PageUtils;
import com.ge.common.utils.Query;
import com.ge.common.utils.R;
import com.ge.modules.api.apiDTO.TbTechnicianDto;
import com.ge.modules.api.service.TbCommissionIncomeinfoService;
import com.ge.modules.api.service.TbOrderService;
import com.ge.modules.api.service.TbTechnicianService;
import com.ge.modules.entity.TbCommissionIncomeinfoEntity;
import com.ge.modules.entity.TbTechnicianEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 技师
 *
 * @author lanyonglin
 * @email www.baidu.com
 * @date 2018-04-20 17:02:43
 */
@RestController
@RequestMapping("/api/technician")
public class TbTechnicianController {
    @Autowired
    private TbTechnicianService technicianService;

    @Autowired
    private TbOrderService tbOrderService;

    @Autowired
    private TbCommissionIncomeinfoService tbCommissionIncomeinfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<TbTechnicianEntity> technicianList = technicianService.queryList(query);
        int total = technicianService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(technicianList, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }


//    /**
//     * 信息
//     */
//    @RequestMapping("/info/{technicianId}")
//    public R info(@PathVariable("technicianId") Integer technicianId) {
//        TbTechnicianEntity technician = technicianService.queryByTechnicianId(technicianId);
//        return R.ok().put("technician", technician);
//    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody TbTechnicianEntity technician) {
        technicianService.save(technician);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TbTechnicianEntity technician) {
        technicianService.update(technician);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] technicianIds) {
        technicianService.deleteBatch(technicianIds);
        return R.ok();
    }


    /**
     * 查询技师信息 统计总订单 总业绩 总客户
     *
     * @param tbTechnicianEntity
     * @return
     */
    @GetMapping("/technicianInfo")
    public R technicianInfo(@LoginTechnicianApi TbTechnicianEntity tbTechnicianEntity) {
        //统计总订单 查询所有订单
        Map<String, Object> map = new HashMap<>();
        map.put("commodityMasterId", tbTechnicianEntity.getTechnicianId());
        map.put("orderStatus", 5);
        int totalOne = tbOrderService.queryTotal(map);
        //统计总业绩 查询订单状态为5 已完成的订单
//        BigDecimal totalTwo = tbOrderService.queryTotal(map);
        //查询提成总金额
        TbCommissionIncomeinfoEntity tbCommissionIncomeinfoEntity = new TbCommissionIncomeinfoEntity();
        tbCommissionIncomeinfoEntity.setUserId(tbTechnicianEntity.getTechnicianId());
        tbCommissionIncomeinfoEntity.setUserType(tbTechnicianEntity.getUserType());
        BigDecimal totalTwo = tbCommissionIncomeinfoService.sumIncomeMoney(tbCommissionIncomeinfoEntity);
        //统计总客户
        Map<String, Object> map1 = new HashMap<>();
        map1.put("commodityMasterId", tbTechnicianEntity.getTechnicianId());
        int totalUsers = tbOrderService.countStoreUsers(map1);
        TbTechnicianDto tbTechnicianDto = tbTechnicianEntity.toDto();
        tbTechnicianDto.setTotalCompleteOrders(totalTwo);
        tbTechnicianDto.setTotalOrders(totalOne);
        tbTechnicianDto.setTotalUsers(totalUsers);
        return R.ok().put("data", tbTechnicianDto);
    }

}
